package com.king.controller;

import com.king.pojo.User;
import com.king.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.Date;

/**
 * com.king.controller
 *
 * @Description: Controller
 * @Author: kuaipeng
 * @Created: on 2019/12/05
 */
@RestController
public class HelloController {
    @Autowired
    private DataSource dataSource;

    @Value("${itcast.url}")
    private String itcastUrl;

    @Value("${itheima.url}")
    private String itheimaUrl;

    @Autowired
    private UserService userService;

    @GetMapping("hello")
    public String hello(){
        System.out.println(" itcastUrl = " + itcastUrl);
        System.out.println(" itheimaUrl = " + itheimaUrl);
        System.out.println(" DataSource = " + dataSource);
        return "Hello, Spring Boot!";
    }

    /**
     * @description: 新增用户
     */
    @GetMapping("/user/add/{user}")
    public void addUser(@PathVariable User user) {
        user.setUserName("test");
        user.setName("test25");
        user.setAge(14);
        user.setPassword("12345678");
        user.setSex(2);
        user.setCreated(new Date());
        userService.addUser(user);
    }

    /**
     * @description: 根据用户id查询用户
     * @param： id 用户id
     * @return: com.king.pojo.User 用户
     */
    @GetMapping("/user/query/{id}")
    public User queryById(@PathVariable Long id){
        return userService.queryById(id);
    }

    /**
     * @description: 根据用户id修改用户
     */

    @GetMapping("/user/update/{user}")
    public void updateUser(@PathVariable User user){
        user.setId(14L);
        user.setUserName("test");
        user.setName("test25");
        user.setAge(14);
        user.setPassword("12345678");
        user.setSex(2);
        user.setCreated(new Date());
        userService.updateUser(user);
    }

    /**
     * @description: 根据用户id删除用户
     * @param： id 用户id
     * @return:
     */
    @GetMapping("/user/deleteById/{id}")
    public void deleteUser(@PathVariable Long id){
            userService.deleteUser(id);
    }



}
